package org.jbit.biz;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.jbit.bean.city;
import org.jbit.bean.county;
import org.jbit.dao.AddressDao;

/**
 * Servlet implementation class GPSInfoEditAction
 */
@WebServlet("/GPSInfoEditAction")
public class AddressEditAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddressEditAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if (method.equals("getCity")){
			String provinceId = request.getParameter("province"); 
			AddressDao AD=new AddressDao();
			List<city> list=new ArrayList<city>();
			list=AD.getcity(provinceId);
			JSONObject jsonObject = new JSONObject();
			jsonObject.accumulate("list",list);
			String contentType = "text/text;charset=utf-8";
			response.setContentType(contentType);
			PrintWriter pw = response.getWriter();
			pw.write(jsonObject.toString());
			pw.flush();
			pw.close();
		}
		
		if (method.equals("getCounty")){
			String cityId = request.getParameter("city"); 
			AddressDao AD=new AddressDao();
			List<county> list=new ArrayList<county>();
			list=AD.getcounty(cityId);
			JSONObject jsonObject = new JSONObject();
			jsonObject.accumulate("list",list);
			String contentType = "text/text;charset=utf-8";
			response.setContentType(contentType);
			PrintWriter pw = response.getWriter();
			pw.write(jsonObject.toString());
			pw.flush();
			pw.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
